ALTER           function fn_get_process_flow_by_processstep_id (@process_step_id bigint)
returns  @fn_result Table( ProcessStep_ID bigint, 
			   PS_Type varchar(20), 
			   Process_Type_Name varchar(20), Product_name varchar(50), Product_id INT )
As
begin
	declare @temp_process_step_id bigint	
	declare @next_process_step_id bigint
	declare @flag_present bigint
	
	declare @nextQueryTable Table( ProcessStep_ID bigint )	
	declare @result2 Table( ProcessStep_ID bigint )		
	declare @result Table(ProcessStep_ID bigint, 		
			PS_Type varchar(20), 
			Process_Type_Name varchar(20),
			Product_Name varchar(50), Product_id INT  )	

	declare @cursor1 CURSOR
	declare @cursor2 CURSOR

 	set @cursor1 = cursor for select ProcessStep_ID from Process_Step_Traverse_Table where Next_ProcessStep_ID = @process_step_id
	
	open @cursor1
		fetch next from @cursor1 into @next_process_step_id
		while @@fetch_status = 0
		begin
			-- Old processStep_Ids are deleted here because processStep_ids should be in order
			-- delete from @result2 where ProcessStep_ID = @next_process_step_id
			-- Insert the ProcessStepIds into result Table
			insert into @result2 (ProcessStep_ID) values ( @next_process_step_id ) 
			-- Insert into NextQueryTable so that they can be iterated to get Next ProcessStepIds
			insert into @nextQueryTable (ProcessStep_ID) values ( @next_process_step_id ) 
			
			fetch next from @cursor1 into @next_process_step_id
			if @@fetch_status <> 0
			begin 
				close @cursor1
				deallocate @cursor1
				-- The Cursor is declared Static else if the NextQueryTable Values Change the Cursor is being Changed
				set @cursor1 = cursor STATIC for select ProcessStep_ID from Process_Step_Traverse_Table where Next_ProcessStep_ID in ( select ProcessStep_ID from @nextQueryTable )
				
				open @cursor1
				fetch next from @cursor1 into @next_process_step_id
				delete from @nextQueryTable
			end
		end
		
	close @cursor1
	deallocate @cursor1
	-- Cursor is made Scrollable becuase we need the Values in order from FAB to Finished Goods
	set @cursor2 = CURSOR SCROLL for select ProcessStep_ID from @result2 	
	open @cursor2
	fetch LAST from @cursor2 into @temp_process_step_id 
	while @@fetch_status = 0
	begin
		set @flag_present = 0
		select @flag_present = ProcessStep_ID from @result where ProcessStep_ID = @temp_process_step_id
		
		if @flag_present is null or @flag_present = 0 
		begin
			insert @result select Product.ProcessStep_ID, PS_Type, PS_Type_Name, Part_Number, Product_id from product, process_step where product.processStep_ID = process_step.processStep_ID and product.processStep_ID = @temp_process_step_id
		end
		fetch PRIOR from @cursor2 into @temp_process_step_id
	end

	close @cursor2
	deallocate @cursor2
	
	-- Insert the values of the product on which the function is called
	insert @result select Product.ProcessStep_ID, PS_Type, PS_Type_Name, Part_Number, Product_id from product, process_step where product.processStep_ID = process_step.processStep_ID and product.processStep_ID = @process_step_id
	
	insert @fn_result select * from @result
	return
end 
